package com.op.teachingliteapp.sender.handler;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import com.op.teachingliteapp.sender.Constants;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

/**
 * Created by Genji on 2015/5/19.
 */
public class SendCommandHandler extends ChannelHandlerAdapter {
    private Context context;
    private  byte[] command;

    public SendCommandHandler(Context context, String commandString) {
        this.context = context;
        this.command = (commandString).getBytes();
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Log.i("sendcomd-active", "------------");
        ByteBuf byteBuf = Unpooled.buffer(this.command.length
        );
        byteBuf.writeBytes(command);
        ctx.writeAndFlush(byteBuf);
        Log.i("sendcomd-active-end", "------------");
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        Log.i("sendcomd-read", "------------");
        String res = (String) msg;
        Toast.makeText(context,res,Toast.LENGTH_SHORT).show();
        Log.i("sendcomd-read-end", "------------");
    }
}
